import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;


public class Score {

	protected Shell shell;
	private Table table;
	private Display display = Display.getDefault();
	private TableColumn Pseudo;
	private TableColumn Temps;
	private TableItem tableItem;
	private Button btnReset;
	private Button btnFermer;
	
	private String monPseudo, monTemps;
	
	private Color blue = display.getSystemColor(SWT.COLOR_CYAN);

	/**
	 * Launch the application.
	 * @param args
	 */
	public Score(String pseudo, String temps) {
		
		monPseudo = pseudo;
		monTemps= temps;
		try {
			this.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(292, 347);
		shell.setText("SWT Application");
		shell.setLayout(null);
		Monitor primary = display.getPrimaryMonitor();
		 Rectangle bounds = primary.getBounds();
	        Rectangle rect = shell.getBounds();
		 int x = bounds.x + (bounds.width - rect.width) / 2;
	        int y = bounds.y + (bounds.height - rect.height) / 2;
	        shell.setLocation(x, y);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 10, 257, 252);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		Pseudo = new TableColumn(table, SWT.NONE);
		Pseudo.setWidth(125);
		Pseudo.setText("Pseudo");
		
		Temps = new TableColumn(table, SWT.NONE);
		Temps.setWidth(125);
		Temps.setText("Temps");
		
		btnReset = new Button(shell, SWT.NONE);
		btnReset.setEnabled(true);
		btnReset.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				effacerScore();
				btnReset.setEnabled(false);
			}
		});
		btnReset.setBounds(10, 268, 84, 25);
		btnReset.setText("Remise \u00E0 z\u00E9ro");
		
		btnFermer = new Button(shell, SWT.NONE);
		btnFermer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		btnFermer.setBounds(192, 268, 75, 25);
		btnFermer.setText("Fermer");
		
		afficherScore();

	}
	
public void afficherScore() {
		
		try {
			// The newInstance() call is a work around for some
			// broken Java implementations
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			} catch (Exception ex) {
			// handle the error
				System.out.println("erreur");
			}
		String url = "jdbc:odbc:Sudoku";
		
		
			Connection con;
			try {
				con = DriverManager.getConnection(url,"" ,"" );
				String requete = "SELECT * FROM Score ORDER BY Temps ASC";
				
				java.sql.Statement stmt = con.createStatement();
				
				
				ResultSet rs = stmt.executeQuery(requete);
				
				
				 while (rs.next()) { 
					 	tableItem = new TableItem(table, SWT.NONE);
					 	
						tableItem.setText(0, rs.getString(2));
						tableItem.setText(1, rs.getString(3));
						
						if(monPseudo.equals(tableItem.getText(0)) & monTemps.equals(tableItem.getText(1)))
						{
							tableItem.setBackground(blue);
						}
                     };
                     
                
               
                     
                     con.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
			
				e.printStackTrace();
			}
			
			
		}

private void effacerScore() {
	try {
		// The newInstance() call is a work around for some
		// broken Java implementations
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
		} catch (Exception ex) {
		// handle the error
			System.out.println("erreur");
		}
	String url = "jdbc:odbc:Sudoku";
	
	
		Connection con;
		try {
			con = DriverManager.getConnection(url,"" ,"" );
			String requete = "DELETE FROM Score";
			
			java.sql.Statement stmt = con.createStatement();
			
			stmt.execute(requete);
			
           con.close();
           
           
           shell.dispose();
           display.readAndDispatch();
           
           open();
           
        
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		
			e.printStackTrace();
		}
}
}
